<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 条件控制</h1>
        <p>Python条件语句是通过一条或多条语句的执行结果（True或者False）来决定执行的代码块。</p>
        <p>
            可以通过下图来简单了解条件语句的执行过程:</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/if-condition.jpg"></p>

        <h2>if 语句</h2>
        <p>Python中if语句的一般形式如下所示：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">if</span><span class="hl-code"> </span><span
                        class="hl-identifier">condition_1</span><span class="hl-code">:
    </span><span class="hl-identifier">statement_block_1</span><span class="hl-code">
</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">condition_2</span><span
                        class="hl-code">:
    </span><span class="hl-identifier">statement_block_2</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
    </span><span class="hl-identifier">statement_block_3</span></pre>
            </div>
        </div>
        <ul>
            <li>如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句</li>
            <li>如果 "condition_1" 为False，将判断 "condition_2"</li>
            <li>如果"condition_2"
                为 True 将执行 "statement_block_2" 块语句
            </li>
            <li>如果 "condition_2" 为False，将执行"statement_block_3"块语句</li>
        </ul>
        <p>
            Python 中用 <b>elif</b> 代替了 <b>else if</b>，所以if语句的关键字为：<b>if – elif – else</b>。
        </p>
        <p>
            <strong>注意：</strong></p>
        <ul>
            <li>
                1、每个条件后面要使用冒号（:），表示接下来是满足条件后要执行的语句块。
            </li>
            <li>
                2、使用缩进来划分语句块，相同缩进数的语句在一起组成一个语句块。
            </li>
            <li>
                3、在Python中没有switch – case语句。
            </li>
        </ul>

        <h3>实例</h3>
        <p>以下是一个简单的 if 实例：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">var1</span><span class="hl-code"> = </span><span class="hl-number">100</span><span
                        class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">var1</span><span
                        class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - if 表达式条件为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">var1</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">var2</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">var2</span><span
                        class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - if 表达式条件为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">var2</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">Good bye!</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span
                class="pun">表达式条件为</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
</span><span class="lit">100</span><span class="pln">
</span><span class="typ">Good</span><span class="pln"> bye</span><span class="pun">!</span></pre>
        <p>从结果可以看到由于变量 var2 为 0，所以对应的 if 内的语句没有执行。</p>
        <p>以下实例演示了狗的年龄计算判断：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">请输入你家狗狗的年龄: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span
                        class="hl-code"> &lt; </span><span class="hl-number">0</span><span class="hl-code">:
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">你是在逗我吧!</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span
                        class="hl-code"> == </span><span class="hl-number">1</span><span class="hl-code">:
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">相当于 14 岁的人。</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span
                        class="hl-code"> == </span><span class="hl-number">2</span><span class="hl-code">:
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">相当于 22 岁的人。</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span
                        class="hl-code"> &gt; </span><span class="hl-number">2</span><span class="hl-code">:
    </span><span class="hl-identifier">human</span><span class="hl-code"> = </span><span
                        class="hl-number">22</span><span class="hl-code"> + </span><span
                        class="hl-brackets">(</span><span class="hl-identifier">age</span><span
                        class="hl-code"> -</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span
                        class="hl-code">*</span><span class="hl-number">5</span><span class="hl-code">
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">对应人类年龄: </span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">human</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment">### 退出提示</span><span class="hl-code">
</span><span class="hl-builtin">input</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">点击 enter 键退出</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>将以上脚本保存在dog.py文件中，并执行该脚本：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3 dog</span><span class="pun">.</span><span
                class="pln">py
</span><span class="pun">请输入你家狗狗的年龄:</span><span class="pln"> </span><span class="lit">1</span><span class="pln">

</span><span class="pun">相当于</span><span class="pln"> </span><span class="lit">14</span><span class="pln"> </span><span
                class="pun">岁的人。</span><span class="pln">
</span><span class="pun">点击</span><span class="pln"> enter </span><span class="pun">键退出</span></pre>
        <p>
            以下为if中常用的操作运算符:</p>

        <table class="reference">
            <tbody>
            <tr>
                <th>操作符</th>
                <th>描述</th>
            </tr>
            <tr>
                <td><code>&lt;</code></td>
                <td>小于</td>
            </tr>
            <tr>
                <td><code>&lt;=</code></td>
                <td>小于或等于</td>
            </tr>
            <tr>
                <td><code>&gt;</code></td>
                <td>大于</td>
            </tr>
            <tr>
                <td><code>&gt;=</code></td>
                <td>大于或等于</td>
            </tr>
            <tr>
                <td><code>==</code></td>
                <td>等于，比较对象是否相等</td>
            </tr>
            <tr>
                <td><code>!=</code></td>
                <td>不等于</td>
            </tr>
            </tbody>
        </table>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-comment"># 程序演示了 == 操作符</span><span class="hl-code">
</span><span class="hl-comment"># 使用数字</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-number">5</span><span
                        class="hl-code"> == </span><span class="hl-number">6</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-comment"># 使用变量</span><span class="hl-code">
</span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">5</span><span
                        class="hl-code">
</span><span class="hl-identifier">y</span><span class="hl-code"> = </span><span class="hl-number">8</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">x</span><span class="hl-code"> == </span><span
                        class="hl-identifier">y</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>
            以上实例输出结果：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">False</span><span class="pln">
</span><span class="kwd">False</span></pre>
        <p>high_low.py文件演示了数字的比较运算：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3 </span><span class="hl-code">

</span><span class="hl-comment"># 该实例演示了数字猜谜游戏</span><span class="hl-code">
</span><span class="hl-identifier">number</span><span class="hl-code"> = </span><span class="hl-number">7</span><span
                        class="hl-code">
</span><span class="hl-identifier">guess</span><span class="hl-code"> = -</span><span class="hl-number">1</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">数字猜谜游戏!</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-identifier">guess</span><span
                        class="hl-code"> != </span><span class="hl-identifier">number</span><span class="hl-code">:
    </span><span class="hl-identifier">guess</span><span class="hl-code"> = </span><span
                        class="hl-builtin">int</span><span class="hl-brackets">(</span><span
                        class="hl-builtin">input</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">请输入你猜的数字：</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">

    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span
                        class="hl-identifier">guess</span><span class="hl-code"> == </span><span class="hl-identifier">number</span><span
                        class="hl-code">:
        </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">恭喜，你猜对了！</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span
                        class="hl-identifier">guess</span><span class="hl-code"> &lt; </span><span
                        class="hl-identifier">number</span><span class="hl-code">:
        </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">猜的数字小了...</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span
                        class="hl-identifier">guess</span><span class="hl-code"> &gt; </span><span
                        class="hl-identifier">number</span><span class="hl-code">:
        </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">猜的数字大了...</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>
            执行以上脚本，实例输出结果如下：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3 high_low</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">数字猜谜游戏!</span><span class="pln">
</span><span class="pun">请输入你猜的数字：</span><span class="lit">1</span><span class="pln">
</span><span class="pun">猜的数字小了...</span><span class="pln">
</span><span class="pun">请输入你猜的数字：</span><span class="lit">9</span><span class="pln">
</span><span class="pun">猜的数字大了...</span><span class="pln">
</span><span class="pun">请输入你猜的数字：</span><span class="lit">7</span><span class="pln">
</span><span class="pun">恭喜，你猜对了！</span></pre>


        <h2>if 嵌套</h2>
        <p>
            在嵌套 if 语句中，可以把 if...elif...else 结构放在另外一个 if...elif...else 结构中。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">if</span><span class="pln"> </span><span
                class="pun">表达式</span><span class="lit">1</span><span class="pun">:</span><span class="pln">
    </span><span class="pun">语句</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">表达式</span><span
                class="lit">2</span><span class="pun">:</span><span class="pln">
        </span><span class="pun">语句</span><span class="pln">
    </span><span class="kwd">elif</span><span class="pln"> </span><span class="pun">表达式</span><span class="lit">3</span><span
                class="pun">:</span><span class="pln">
        </span><span class="pun">语句</span><span class="pln">
    </span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
        </span><span class="pun">语句</span><span class="pln">
</span><span class="kwd">elif</span><span class="pln"> </span><span class="pun">表达式</span><span
                class="lit">4</span><span class="pun">:</span><span class="pln">
    </span><span class="pun">语句</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
    </span><span class="pun">语句</span></pre>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># !/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">num</span><span class="hl-code">=</span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入一个数字：</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code">%</span><span class="hl-number">2</span><span class="hl-code">==</span><span
                        class="hl-number">0</span><span class="hl-code">:
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code">%</span><span class="hl-number">3</span><span class="hl-code">==</span><span
                        class="hl-number">0</span><span class="hl-code">:
        </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">你输入的数字可以整除 2 和 3</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code">:
        </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">你输入的数字可以整除 2，但不能整除 3</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code">%</span><span class="hl-number">3</span><span class="hl-code">==</span><span
                        class="hl-number">0</span><span class="hl-code">:
        </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">你输入的数字可以整除 3，但不能整除 2</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code">:
        </span><span class="hl-identifier">print</span><span class="hl-code">  </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">你输入的数字不能整除 2 和 3</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>将以上程序保存到 test_if.py 文件中，执行后输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3 test</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">输入一个数字：</span><span class="lit">6</span><span class="pln">
</span><span class="pun">你输入的数字可以整除</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span
                class="pun">和</span><span class="pln"> </span><span class="lit">3</span></pre>

    </div>
</div>
</body>
</html>
